'use strict';
angular.module('sbAdminApp', ['ngCookies', 'loadingButton'])
	.controller('LoginCtrl', ['$scope', '$rootScope', '$state', '$cookieStore', 'TeacherService',
		function($scope, $rootScope, $state, $cookieStore, TeacherService) {
			$scope.isClicked = false;
			$scope.alertmsg = false;

			$scope.login = function() {
				$scope.isClicked = true;

				TeacherService.authenticate($.param({
					teaNum: $scope.teaNum,
					teaPwd: $scope.teaPwd
				}), function(authenticationResult) {
					console.log(authenticationResult);
					if(authenticationResult.success){
						// 成功
						var authToken = authenticationResult.object.token;
						$rootScope.authToken = authToken;
						
						$cookieStore.put('authToken', authToken);
						console.log($cookieStore.get('authToken'));
						TeacherService.get(function(teacher) {
							$rootScope.teacher = teacher;
							$cookieStore.put('teacher', teacher);
							console.log(teacher);
							$state.go("dashboard.home");
						});
					} else {
						// $state.go("dashboard.home");
						$scope.isClicked = false;
						$scope.alertmsg = true;
					}
				});
			};
		}
	]);